package com.jnu.keepaccounts;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.jnu.keepaccounts.data.DataForAnaylist;

public class MyMarkerView extends MarkerView {
    private TextView textView1,textView2;

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MyMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);
        textView1 = findViewById(R.id.ttt);
        textView2 = findViewById(R.id.ddd);
    }

    public void refreshContent(Entry e, Highlight highlight) {
        DataForAnaylist tem;
        if(e instanceof PieEntry){
            tem = (DataForAnaylist) ((PieEntry)e).getData();
        }
        else tem = (DataForAnaylist) ((BarEntry)e).getData();
        try{
            textView1.setText(tem.getNameOrLaiYuan());
            textView2.setText(tem.getNum()+"("+tem.getBnum()+"%)");
        }catch (Exception exception){
            exception.printStackTrace();
        }
        super.refreshContent(e,highlight);

    }

    public int getXOffset() {
        // this will center the marker-view horizontally
        return -(getWidth() / 2);
    }

    public int getYOffset() {
        // this will cause the marker-view to be above the selected value
        return -getHeight();
    }
    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
        MPPointF offset = getOffset();
        Chart chart = getChartView();
//        if(chart instanceof PieChart)
        float width = getWidth();
        float height = getHeight();
// posY \posX 指的是markerView左上角点在图表上面的位置
//处理Y方向
        if (posY <= height ) {// 如果点y坐标小于markerView的高度，如果不处理会超出上边界，处理了之后这时候箭头是向上的，我们需要把图标下移一个箭头的大小
            offset.y = 0;
        } else {//否则属于正常情况，因为我们默认是箭头朝下，然后正常偏移就是，需要向上偏移markerView高度和arrow size，再加一个stroke的宽度，因为你需要看到对话框的上面的边框
            offset.y = -height; // 40 arrow height   5 stroke width
        }
//处理X方向，分为3种情况，1、在图表左边 2、在图表中间 3、在图表右边
//
        if (posX > chart.getWidth() - width) {//如果超过右边界，则向左偏移markerView的宽度
            offset.x = -width;
        } else {//默认情况，不偏移（因为是点是在左上角）
            offset.x = 0;
            if (posX > width / 2) {//如果大于markerView的一半，说明箭头在中间，所以向右偏移一半宽度
                offset.x = -(width / 2);
            }
        }
        return offset;
    }



}
